/*************************************
 * @author Tianzhen(uimeet.com)
 *
 * 首页的大小图
 * */
(function () {
    
    function Slide(rawTrigger, conf) {
    
        var self = this,
            trigger = $(rawTrigger),
            tit = trigger.find(conf.title),
            img = trigger.find(conf.img),
            txt = trigger.find(conf.text),
            w = img.width(),
            h = img.height();
        
        
        $.extend(self, {
            load: function() {
                this.tit_show(true);
                this.txt_show(false, true);
                this.bind_events();
            },
            bind_events: function() {
                trigger.bind('mouseover', self.txt_show_mode)
                    .bind('mouseout', self.tit_show_mode);
            },
            tit_show_mode: function() { self.tit_show(); },
            txt_show_mode: function() { self.txt_show(true, false) },
            tit_show: function (locate) {
                tit.show();txt.hide();
                if (locate) {
                    tit.css({
                        'position': 'absolute',
                        'left': 0,
                        'top': h - tit.height(),
                        'width': w
                    });
                }

            },
            txt_show: function(show, locate) {
                if (show) {
                    txt.show();
                    tit.hide();
                }
                if (locate) {
                    txt.css({
                        'position': 'absolute',
                        'left': 0,
                        'top': 0,
                        'width': w,
                        'height': h
                    });
                    if ($.browser.msie) {
                        txt.css('opacity', .8);
                    }
                }
            }
        });

        self.load();

    };
    
    $.fn.slide = function (conf) {
        
        this.each(function () {
            new Slide(this, conf);
        });

    };

})(jQuery);

/*****************************************
 * @author Tianzhen(uimeet.com)
 *
 * 首页的滚动显示
 * */
(function ($) {

    function Scrollable(rawTrigger, conf) {
        
        var self = this,
            trigger = $(rawTrigger),
            prev = $(conf.prev),
            next = $(conf.next),
            newitems = $(conf.newitems);
            page = 0,
            loaded = [0],
            totalrecords = $.intval(trigger.attr('totalrecords'));
        
        var api = trigger.scrollable({
                'items': conf.items, 
                //'circular': true,
                'next': '._n'
        });
        
        $.extend(self, {
            load: function () {
                this.bindEvents();
            },
            bindEvents: function () {
                prev.bind('click', this.prev);
                next.bind('click', this.next);
            },
            bindData: function () {
                page++;
                page = Math.min(page, this.totalPages() - 1);
                if ($.inArray(page, loaded) == -1) {
                    execute('load_partners'
                        , {'page': page}
                        , this.bindData_callback);
                }
            },
            bindData_callback: function(response) {
                if (response != 0) {
                    eval('var json = ' + response);
                    if (json.totalrecords > 0) {
                        newitems.html(json.result);
                        trigger.data('scrollable').addItem(newitems.find('ul')).next();
                    }
                    totalrecords = json.totalrecords;
                    // 已经读取过
                    loaded.push(page)
                }
            },
            prev: function() {
                page--;
                page = Math.max(0, page);
                trigger.data('scrollable').prev();
                self.disabledPager();
            },
            next: function() {
                self.bindData();
                trigger.data('scrollable').next();
                self.disabledPager();
            },
            disabledPager: function () {
                if (page < 1) {
                    // 禁用上一页按钮
                    prev.addClass('disabled');
                } else {
                    prev.removeClass('disabled');
                }
                if ((page + 1) >= this.totalPages()) {
                    next.addClass('disabled');
                } else {
                    next.removeClass('disabled');
                }
            },
            totalPages: function() {
                return Math.ceil(totalrecords / 10);
            }
        });

        self.load();

    };

    $.fn.xbtscrollable = function (conf) {
        
        this.each(function () {
            new Scrollable(this, conf);
        });

    };

})(jQuery);

function showWeiboFollowButton() {
    var container = $('#weibo_follow_container');
    container.show();
    container.find('iframe').attr('src', 'http://widget.weibo.com/relationship/followbutton.php?language=zh_cn&width=136&height=24&uid=2608526213&style=1&btn=red&dpc=1');
};
// 获取已经显示出来的灵感信息的id
function resloveShowedAfflatus() {
    return $('.wat_box').attrs('related').join(',');
};
// ready
$(function () {
    var container = $('#boxes');
    container.imagesLoaded(function() {
        container.masonry({
            'itemSelector': '.wat_box',
            'gutterWidth': 10
        });
    });

    container.infinitescroll({
            navParams: {'exclude': resloveShowedAfflatus()},
            //navSelector : '#infscr_nav',
            //nextSelector : '#infscr_nav ._infscr',
            navSelector : '#page-nav',
            nextSelector : '#page-nav a',
            itemSelector : '.wat_box',
            loading: {
                    finishedMsg: '下面没有了，敬请期待。',
                    img: $STATIC_PREFIX + '/images/common/loading_black.gif',
                    msgText: '努力加载中，请稍候...'
                },
            errorCallback: function () {
                $('#infscr_nav').fadeOut('normal', function () {
                    $('#infscr_finished').fadeIn('normal');
                    
                });
                container.data('infinitescroll')
                            .options
                            .loading
                            .msg
                            .find('img')
                            .parent()
                            .hide();
            }
        },
        function (newElements) {
            var newElems = $(newElements).css({ opacity: 0 });
            newElems.imagesLoaded(function() {
                newElems.animate({ opacity: 1 });
                container.masonry('appended', newElems, true);
            });
            var instance = container.data('infinitescroll');
            instance.update({
                'navParams': {
                    'exclude': resloveShowedAfflatus()
                }
            });
            //$(instance.options.navSelector).show();
        }
    );
    //$(window).unbind('.infscr');
    $('._infscr').click(function () { 
        container.data('infinitescroll').scroll();
        return false;
    });

    $('._slide').slide({
        'title': '._slide_tit',
        'text': '._slide_txt',
        'img': 'img'
    });
    $('.scrollable').xbtscrollable({
        'prev': '.pre',
        'next': '.next',
        'items': '.main',
        'newitems': '#newitems'
    });
    // 延迟加载微博关注按钮
    setTimeout('showWeiboFollowButton();', 1000);
    // 特惠信息性价比指数
    $('.rating').stars();
    // 目的地检索框
    $('#home_dest_search').value_opr();
    // 首页的tabs
    $('.tabs a').hover(function () {
        var self = $(this);
        var lastOn = $('.tabs a.on');
        if (self.html() != lastOn.html()) {
            // 移除之前选中的
            lastOn.removeClass('on');
            // 隐藏之前的列表
            $('.type' + lastOn.attr('type')).hide();
            // 添加自己的选中抓状态
            self.addClass('on');
            // 显示当前的
            $('.type' + self.attr('type')).show();
        }
    });
});

